** Regressions

log using "$project_path/output/appendix_tables/Appendix_Table_C8.smcl", replace

use "$project_path/data/4_regdata/regdata.dta", clear

// Preparing variables
gen decade = Year - mod(Year, 10)
tab decade, gen(d_decade_)
tab Region, gen(d_region_)
local covariates_list = "d_decade_1-d_decade_8 d_region_1-d_region_6 last_runvar last_treatment"
gen mis_last_runvar = (last_runvar==.)
gen mis_last_treatment = (last_treatment==.)
replace last_runvar = 0 if mis_last_runvar==1
replace last_treatment = 0 if mis_last_treatment==1
foreach v in growth_gdpc_penn inflation_imf unemployment_ilo trade_wb hdi democracy_index {
	forvalues k = 1/3 {
		gen mis_`v'_`k'lag = (`v'_`k'lag==.)
		replace `v'_`k'lag = 0 if mis_`v'_`k'lag==1
		local covariates_list = "`covariates_list' `v'_`k'lag mis_`v'_`k'lag"
	}
}

sort Country Year Month Type_Election
set seed 1234

// Test for the full sample 
rdperm runvar `covariates_list', p(50000)

local rdperm_res_full_sample: di %5.3f r(pval)

// Test for subsamples
foreach subsample in pres parl democ autoc freefair not_freefair {
	rdperm runvar `covariates_list' if subsample_`subsample'==1, p(50000)
	local rdperm_res_`subsample': di %5.3f r(pval)
}

log close

** Building .tex table

file open myfile using "$project_path/output/appendix_tables/Appendix_Table_C8.tex", write replace
file write myfile "\begin{tabular}{lr}" _n
file write myfile "\hline" _n
file write myfile "Sample & p-val. \\" _n
file write myfile "\hline" _n
file write myfile "Full sample & `rdperm_res_full_sample'\\" _n
file write myfile "Presidential & `rdperm_res_pres'\\" _n
file write myfile "Parliamentary & `rdperm_res_parl'\\" _n
file write myfile "Free and fair & `rdperm_res_freefair'\\" _n
file write myfile "Not free and fair & `rdperm_res_not_freefair'\\" _n
file write myfile "\hline" _n
file write myfile "\end{tabular}" _n
file close myfile

